import {
  mapState,
  mapGetters
} from 'vuex'
export default {
  computed: {
    ...mapState('user', ['token']),
    ...mapGetters('cart', ['goodsCount'])
  },
  watch: {

    // 监听goodsCount变化，重新设置角标
    goodsCount(newVal) {
      console.log(newVal);
      if (newVal === undefined || newVal === '') return
      uni.setTabBarBadge({
        index: 2,
        text: newVal + '' // text值一定要是字符串
      })
    }
  },
  async onShow() {
    // 如果登录了，则设置购物车角标数据信息
    if (this.token) {
      await this.$store.dispatch('cart/getInitData')
      uni.setTabBarBadge({
        index: 2,
        text: this.goodsCount + '' // text值一定要是字符串
      })
    }
  },
}
